package com.fast.develop.management.resource.utils;

import cn.hutool.core.util.StrUtil;

/**
 * @author 莫寅
 * @createTime 2022-06-16 22:20
 * @desc：
 */
public class SchemaFileUtil {

    public static final String SCHEMA_SUFFIX = ".json";

    /**
     * 从路径上获取文件名
     * @param path
     * @return
     */
    public static String getPathName(String path){
        String name = null;
        if(path.indexOf(StrUtil.SLASH) == -1){
            name = path;
        }else{
            String[] props = path.split(StrUtil.SLASH);
            name = props[props.length-1];
        }
        //删除后缀名
        name = removeSuffix(name);
        return name;
    }

    /**
     * 删除后缀
     * @param name
     * @return
     */
    public static String removeSuffix(String name){
        int dotIndex = name.indexOf(StrUtil.DOT);
        if(dotIndex != -1){
            return name.substring(0, dotIndex);
        }else{
            return name;
        }
    }


    public static boolean hasSuffix(String path){
        if(path.indexOf(SCHEMA_SUFFIX) != -1){
            return true;
        }else{
            return false;
        }
    }

    public static String getSuffix(String path){
        int suffixIndex = path.indexOf(SCHEMA_SUFFIX);
        int metaMarkIndex = path.indexOf("#");
        if(suffixIndex != -1 && metaMarkIndex == -1){
            return path.substring(suffixIndex, path.length());
        }else if(suffixIndex != -1 && metaMarkIndex != -1){
            return path.substring(suffixIndex, metaMarkIndex);
        }else{
            return null;
        }
    }


    public static String getFilePath(String path){
        int suffixIndex = path.indexOf(SCHEMA_SUFFIX);
        int metaMarkIndex = path.indexOf("#");
        if(suffixIndex != -1 && metaMarkIndex != -1){
            return path.substring(0, metaMarkIndex);
        }else{
            return path;
        }
    }

    public static String createPath(String name){
        int suffixIndex = name.indexOf(SCHEMA_SUFFIX);
        if(suffixIndex == -1){
            return name + SCHEMA_SUFFIX;
        }else{
            return name;
        }
    }

    public static String getSchemaNodePath(String path){
        int metaMarkIndex = path.indexOf("#");
        if(metaMarkIndex != -1){
            return path.substring(metaMarkIndex, path.length());
        }else{
            return null;
        }
    }


    public static String getSchemaType(String path){
        int metaMarkIndex = path.indexOf("#");
        if(metaMarkIndex != -1){
            String nodePath = path.substring(metaMarkIndex, path.length());
            return nodePath.split(StrUtil.SLASH)[1];
        }else{
            return null;
        }
    }

    /**
     * 是否是文件
     * 例：company.json 为文件
     * 例 company.json#/field
     * @param path
     * @return
     */
    public static boolean isFile(String path){
        if(path.indexOf(SCHEMA_SUFFIX) != -1){
            return true;
        }else{
            return false;
        }
    }



    /**
     * 是否有schema节点路径
     * 例 company.json#/field
     * @param path
     * @return
     */
    public static boolean isSchemaNodePath(String path){
        if(path.indexOf(SCHEMA_SUFFIX) != -1 && path.indexOf("#") != -1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取节点名称
     * @param path
     * @return
     */
    public static String getSchemaNodeName(String path){
        String nodePath = getSchemaNodePath(path);

        if(isSchemaNodePath(path)){
            String[] props = nodePath.split("/");
            String nodeName = props[props.length-1];
            return nodeName;
        }else{
            return null;
        }
    }

    public static boolean filterFile(String path){
        if(path.indexOf(".mark") != -1){
            return false;
        }else if(path.indexOf(".DS_Store") != -1){
            return false;
        }else{
            return true;
        }
    }

    public static boolean filterFolder(String path){
        if(StrUtil.isBlank(path)){
            return false;
        }else{
            return true;
        }
    }
}
